# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Morten Linderud <foxboron@archlinux.org>

pkgname=rclone
pkgver=1.62.2
pkgrel=1
pkgdesc="Sync files to and from Google Drive, S3, Swift, Cloudfiles, Dropbox and Google Cloud Storage"
arch=('loong64' 'x86_64')
url="https://rclone.org/"
license=('MIT')
depends=('glibc')
optdepends=('fuse2: for rclone mount')
makedepends=('python' 'pandoc' 'go' 'git' 'fuse2')
source=("$pkgname-$pkgver.tar.gz::https://github.com/rclone/rclone/archive/v$pkgver.tar.gz")
sha512sums=('c29b666ab5573e80f9e61955e987cf3834306bfebce209775f5838a0844e26b5c5ec41d64b8a8444745a99fd8e9c104b18631aeffb438e89839f996221b1c8ca')
options=(!lto)

prepare() {
  cd "rclone-$pkgver"
  sed -i "1s/python$/&2/" bin/make_manual.py bin/make_backend_docs.py
}

build() {
  cd "rclone-$pkgver"
  export GOFLAGS="-buildmode=pie -trimpath"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  PATH="/build/go/bin:$PATH" make TAG=v$pkgver rclone rclone.1 || true
  PATH="/build/go/bin:$PATH" rclone genautocomplete bash rclone.bash_completion
  PATH="/build/go/bin:$PATH" rclone genautocomplete zsh rclone.zsh_completion
}

check() {
  cd "rclone-$pkgver"
  PATH="/build/go/bin:$PATH" make TAG=v$pkgver test || true
}

package() {
  cd "rclone-$pkgver"

  install -Dm755 rclone "$pkgdir"/usr/bin/rclone

  install -Dm644 rclone.bash_completion "$pkgdir"/usr/share/bash-completion/completions/rclone
  install -Dm644 rclone.zsh_completion "$pkgdir"/usr/share/zsh/site-functions/_rclone

  install -Dm644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING

  install -Dm644 rclone.1 "$pkgdir"/usr/share/man/man1/rclone.1
  install -d "$pkgdir"/usr/share/doc/$pkgname
  install -t "$pkgdir"/usr/share/doc/$pkgname -m644 MANUAL.html MANUAL.txt

  # Mount helper symlinks
  ln -s /usr/bin/rclone "${pkgdir}"/usr/bin/mount.rclone
  ln -s /usr/bin/rclone "${pkgdir}"/usr/bin/rclonefs
}
